package com.chuanye.jiguangimdemo.adapters;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.chuanye.jiguangimdemo.R;
import com.chuanye.jiguangimdemo.app.MyApplication;
import com.chuanye.jiguangimdemo.database.FriendEntry;
import com.chuanye.jiguangimdemo.database.FriendRecommendEntry;
import com.chuanye.jiguangimdemo.entity.Event;
import com.chuanye.jiguangimdemo.entity.EventType;
import com.chuanye.jiguangimdemo.entity.FriendInvitation;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.im.android.api.ContactManager;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.GetUserInfoCallback;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.UserInfo;
import cn.jpush.im.api.BasicCallback;

public class FriendRecommendAdapter extends BaseAdapter {

    private Context mContext;
    private List<FriendRecommendEntry> mList = new ArrayList<>();

    public FriendRecommendAdapter(Context mContext, List<FriendRecommendEntry> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null){
            convertView = View.inflate(mContext, R.layout.item_friend_recomend,null);
            holder = new ViewHolder();
            holder.frirecom_head_icon_iv = convertView.findViewById(R.id.frirecom_head_icon_iv);
            holder.frirecom_name_tv = convertView.findViewById(R.id.frirecom_name_tv);
            holder.frirecom_reason_tv = convertView.findViewById(R.id.frirecom_reason_tv);
            holder.frirecom_agree_btn = convertView.findViewById(R.id.frirecom_agree_btn);
            holder.frirecom_state_tv = convertView.findViewById(R.id.frirecom_state_tv);
            convertView.setTag(holder);

        }
        holder = (ViewHolder) convertView.getTag();

        final FriendRecommendEntry item = mList.get(position);
        String path = item.avatar;
        Log.i("打印头像地址：",""+path);
        if (path == null || TextUtils.isEmpty(path)) {
            holder.frirecom_head_icon_iv.setImageResource(R.drawable.rc_default_portrait);
        } else {
            holder.frirecom_head_icon_iv.setImageResource(R.drawable.rc_default_portrait);
        }
        holder.frirecom_name_tv.setText(item.displayName);
        holder.frirecom_reason_tv.setText(item.reason);

        JMessageClient.getUserInfo(item.username, new GetUserInfoCallback(){

            @Override
            public void gotResult(int i, String s, UserInfo userInfo) {
                if (i == 0) {//成功
                    if (userInfo.isFriend()) {
                        item.state = FriendInvitation.ACCEPTED.getValue();
                        item.save();
                        FriendEntry entry = FriendEntry.getFriend(MyApplication.getUserEntry(), item.username, item.appKey);
                        if (entry == null) {
                            /*EventBus.getDefault().post(new Event.Builder().setType(EventType.addFriend)
                                    .setFriendId(item.getId()).build());*/
                            //添加朋友
                        }

                    }
                }
            }
        });

        if (item.state.equals(FriendInvitation.INVITED.getValue())) {//邀请
            holder.frirecom_agree_btn.setVisibility(View.VISIBLE);
            holder.frirecom_state_tv.setVisibility(View.GONE);
        } else if (item.state.equals(FriendInvitation.ACCEPTED.getValue())) {//已接受
            holder.frirecom_agree_btn.setVisibility(View.GONE);
            holder.frirecom_state_tv.setVisibility(View.VISIBLE);
            holder.frirecom_state_tv.setTextColor(Color.parseColor("#858585"));
            holder.frirecom_state_tv.setText("已添加");
        } else if (item.state.equals(FriendInvitation.INVITING.getValue())) {//等待验证
            holder.frirecom_agree_btn.setVisibility(View.GONE);
            holder.frirecom_state_tv.setVisibility(View.VISIBLE);
            holder.frirecom_state_tv.setTextColor(Color.parseColor("#6fd66b"));
            holder.frirecom_state_tv.setText("等待验证");
            holder.frirecom_state_tv.setTextColor(Color.parseColor("#2DD09C"));
        } else if (item.state.equals(FriendInvitation.BE_REFUSED.getValue())) {//对方已拒绝
            holder.frirecom_agree_btn.setVisibility(View.GONE);
            holder.frirecom_reason_tv.setTextColor(Color.parseColor("#858585"));
            holder.frirecom_state_tv.setVisibility(View.VISIBLE);
            holder.frirecom_state_tv.setTextColor(Color.parseColor("#858585"));
            holder.frirecom_state_tv.setText("对方已拒绝");
        } else {//已拒绝
            holder.frirecom_agree_btn.setVisibility(View.GONE);
            holder.frirecom_state_tv.setVisibility(View.VISIBLE);
            holder.frirecom_state_tv.setTextColor(Color.parseColor("#858585"));
            holder.frirecom_state_tv.setText("已拒绝");
        }

        //同意点击事件
        final ViewHolder finalHolder = holder;
        holder.frirecom_agree_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //final Dialog dialog = DialogCreator.createLoadingDialog(mContext, "正在加载");
                //接受添加
                ContactManager.acceptInvitation(item.username, item.appKey, new BasicCallback() {
                    @Override
                    public void gotResult(int responseCode, String responseMessage) {
                        //dialog.dismiss();
                        if (responseCode == 0) {
                            item.state = FriendInvitation.ACCEPTED.getValue();
                            item.save();
                            finalHolder.frirecom_agree_btn.setVisibility(View.GONE);
                            finalHolder.frirecom_state_tv.setVisibility(View.VISIBLE);
                            finalHolder.frirecom_state_tv.setTextColor(Color.parseColor("#858585"));
                            finalHolder.frirecom_state_tv.setText("已添加");

                            //EventBus.getDefault().post(new Event.Builder().setType(EventType.addFriend).setFriendId(item.getId()).build());

                            //添加好友成功创建个会话
                            Conversation conversation = JMessageClient.getSingleConversation(item.username, item.appKey);
                            if (conversation == null) {
                                conversation = Conversation.createSingleConversation(item.username, item.appKey);
                                //创建会话
                                //EventBus.getDefault().post(new Event.Builder().setType(EventType.createConversation).setConversation(conversation).build());
                            }
                        }
                    }
                });
            }
        });

        return convertView;
    }

    public class ViewHolder{
        ImageView frirecom_head_icon_iv;//头像
        TextView frirecom_name_tv; //用户名
        TextView frirecom_reason_tv;//添加理由
        Button frirecom_agree_btn;//同意按钮
        TextView frirecom_state_tv;//添加状态



    }


}
